package com.example.fw;



import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;


import com.example.tests.ContactObject;


public class ContactHelper extends HelperWithWebDriverBase {
        
        private Set<ContactObject> contactCache;

        public ContactHelper(AppManager manager) {
                super(manager);
                
        }
         public Set<ContactObject> getContacts() {
                 if (contactCache == null){
                         buildContactCache();
                 }
                 return contactCache;
         }
         private void buildContactCache() { 
                  contactCache = new HashSet<ContactObject>();
                 
                  manager.getNavigationHelper().openMainPage();
                  WebElement table = findElements(By.id("maintable")).get(0);
                  List<WebElement> wholeRowList = table.findElements(By.tagName("tr"));
                  List<WebElement> rowList = wholeRowList.subList(1, wholeRowList.size()-1);
//                List<WebElement> rowList = findElements(By.xpath(".//tr[@name]"));
                  
                  
                  for (WebElement row : rowList){
                                                  
                   ContactObject contact = new ContactObject()
                   .setFirstName(row.findElement(By.xpath(".//td[3]")).getText())
                   .setLastName(row.findElement(By.xpath(".//td[2]")).getText())
                   .setId(row.findElement(By.tagName("input")).getAttribute("value"));

                   contactCache.add(contact);
                   }
        }
         
        public void createContact(ContactObject contactObject) {
                
                manager.getNavigationHelper().openMainPage();
                initNewContactCreation();
                fillInContactForm(contactObject, true);
                submitContactCreation();
                manager.getNavigationHelper().openMainPage();
                contactCache = null;
        }
        
        public void deleteContact(ContactObject anyContact) {
                manager.getNavigationHelper().openMainPage();
                markContactCheckbox(anyContact);
                click (By.xpath("//a[@href='edit.php?id="+anyContact.id+"']"));
                click(By.xpath("(//input[@name='update'])[2]"));
                buildContactCache();
        }
        
        public void modifyContact(ContactObject anyContact, ContactObject newContact) {
                manager.getNavigationHelper().openMainPage();
                initContactModification(anyContact);
                fillInContactForm(newContact, false);
                submitContactModification();
                buildContactCache();
        }

        // creation     
        private void initNewContactCreation() {
                click(By.linkText("add new"));
        
        }
        private void fillInContactForm(ContactObject contactObject, boolean fillGroup) {
                type(By.name("firstname"), contactObject.firstname);
                type(By.name("lastname"), contactObject.lastname);
                type(By.name("address"), contactObject.address);

                type(By.name("home"), contactObject.telhome);
                type(By.name("mobile"), contactObject.telmobile);
                type(By.name("work"), contactObject.telwork);
                type(By.name("email"), contactObject.email);
                type(By.name("email2"), contactObject.email2);
                
        select(By.name("bday"), contactObject.birthday);
        select(By.name("bmonth"), contactObject.birthmonth);
        type(By.name("byear"), contactObject.birthyear);

        if (fillGroup) {
                selectRandom(By.name("new_group"));
        } else {
                //.......
        }
//        if (driver.findElements(By.name("new_group")).size() > 0){
//              select(By.name("new_group"), contactObject.group);
//        }

        type(By.name("address2"), contactObject.address2);
        type(By.name("phone2"), contactObject.phone2);
        
        }
        private void submitContactCreation() {
                click(By.name("submit"));
        }       

        // modification
                private void initContactModification(ContactObject anyContact) {
                markContactCheckbox(anyContact);
                click (By.xpath("//a[@href='edit.php?id="+anyContact.id+"']"));
        }
        private void submitContactModification() {
                click(By.name("update"));
        }
        
        private void checkContact(ContactObject anyContact) {
                click(By.name("selected[]"));
        }         
        
        private void markContactCheckbox(ContactObject anyContact) {
                  WebElement element = driver.findElement(By.xpath("//input[@value='"+anyContact.id+"']"));
                  element.click();
        }
        
}
